<?php
//Loader::loadClass('AntiSpam', "modules/icarrent/pnincludes");
class PNReserve extends PNObjectEx {
  function PNReserve($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'icarrent_reserve';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_topics',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_brand',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('brand_name'),
                            'compare_field_table' =>  'brand_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_model',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('model_name'),
                            'compare_field_table' =>  'model_id',
                            'compare_field_join'  =>  'id');
    $this->_objJoin[]     = array ( 'join_table'  =>  'icarrent_shop',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('shop_name'),
                            'compare_field_table' =>  'shop_id',
                            'compare_field_join'  =>  'id');

    //permission for post new | edit topic
    $func = FormUtil::getPassedValue ('func', false);
    $ctrl = FormUtil::getPassedValue ('ctrl', false);
    $this->_objPermission = array('component' => "icarrent:$func:$ctrl",
                                  'instance'  => '.*',
                                  'level'     => USER_EDIT_LEVEL);
    $this->_init($init, $where);
  }

  function updatePreProcess(){
    //check to delete icon or model or video
    $form = FormUtil::getPassedValue ('form', false );
    $id = $this->objData['id'];
    $this->_objData['ipaddress'] = InnoUtil::getIpAddress();

    //$this->_objData['lu_date'] = date("Y-m-d H:i:s");

  }

  function insertPreProcess(){
    //get ip address
    $this->_objData['ipaddress'] = InnoUtil::getIpAddress();
    //$this->checkColor();
  }

  function insertPostProcess(){

    $form = FormUtil::getPassedValue ('form', false );

    //var_dump($form);exit;

    //Insert image
    Loader::loadClass('DataUtilEx', "modules/icarrent/pnincludes");
    $id = DBUtil::getInsertID ($this->_objType, $this->_objField);
    if ($id){
      //set confirmation code
      //get category prefix
      $cate = DBUtil::selectFieldByID('icarrent_category','prefix',$this->_objData['category_id']);
      $subcate = DataUtilEx::fillString($this->_objData['subcategory_id'],2,'0');
      $ref_id = DataUtilEx::fillString($id,5,'0', $cate . $subcate);

      $cr_date = $this->_objData['cr_date'];

      $carproperty_id = $this->_objData['carproperty_id'];
      if($carproperty_id){
        $object = array('id'=>$id,'ref_id'=>$ref_id, 'carproperty_id'=>serialize($carproperty_id),'temp_cr_date'=>$cr_date);
      }else{
        $object = array('id'=>$id,'ref_id'=>$ref_id, 'temp_cr_date'=>$cr_date);
      }


      //$object = array('id'=>$id,'ref_id'=>$ref_id, 'temp_cr_date'=>$cr_date);
      DBUtil::updateObject($object,$this->_objType);

      $imagePath = $this->uploadFiles($id);
      $this->checkHasImage($id);
    }
  }

/*
  function updatePostProcess(){

  }
*/

/*
  function updatePostProcess(){

  }
*/

/*
  function deletePreProcess(){

  }
*/

/*
  function deletePostProcess(){

  }
*/

  //

}